package com.lorenzogao.lib_view

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.lorenzogao.lib_view.ext.sp2px
import kotlin.math.abs

/**
 *  create by LorenzoGao
 *  email: 2508719070@qq.com
 *  TIME: 2019-11-11 13:11
 *  description:
 */

class QQStepView : View {


    private var mOuterColor: Int = Color.RED
    private var mInnerColor: Int = Color.BLUE
    private var mBorderWidth: Int = 20  // 20px
    private var mTestSize: Int = 15
    private var mTextColor: Int = Color.RED

    private val defalutWidthAndHeight = 100


    private var maxValue: Int = 0

    private var mCurrentValue: Int = 0


    private val mOuterPaint: Paint by lazy {
        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = mOuterColor
        paint.strokeWidth = mBorderWidth.toFloat()
        paint.style = Paint.Style.STROKE
        paint.strokeCap = Paint.Cap.ROUND
        paint
    }

    private val mInnerPaint: Paint by lazy {
        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = mInnerColor
        paint.strokeWidth = mBorderWidth.toFloat()
        paint.style = Paint.Style.STROKE // 设置是否空心  STROKE 空心 FILL 实心
        paint.strokeCap = Paint.Cap.ROUND  // 设置绘制的线帽样式

        paint
    }

    private val mTextPaint: Paint by lazy {
        val paint = Paint()
        paint.isAntiAlias = true
        paint.color = mTextColor
        paint.textSize = mTestSize.toFloat()
        paint
    }


    constructor(context: Context?) : this(context, null)

    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)


    /**
     * Step 1 分析效果
     * Step 2 确定自定义属性 编写attrs.xml
     * Step 3 在布局中使用
     * Step 4 在自定义View中使用自定义属性
     * Step 5 onMeasure
     * Step 6 画圆弧  文字
     *
     */
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {

        val array = context?.obtainStyledAttributes(attrs, R.styleable.QQStepView)
        array?.apply {
            mOuterColor = getColor(R.styleable.QQStepView_outerColor, mOuterColor)
            mInnerColor = getColor(R.styleable.QQStepView_innerColor, mInnerColor)
            mBorderWidth = getDimension(R.styleable.QQStepView_borderWidth, mBorderWidth.toFloat()).toInt()
            mTestSize = getDimensionPixelSize(R.styleable.QQStepView_stepTextSize, mTestSize)
            mTextColor = getColor(R.styleable.QQStepView_stepTextColor, mTextColor)
            maxValue = getInt(R.styleable.QQStepView_maxStepValue, maxValue)
            mCurrentValue = getInt(R.styleable.QQStepView_currentStepValue, mCurrentValue)

            recycle()
        }


    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        // 如果是warp_content 给个默认值100
        var widthMode = MeasureSpec.getMode(widthMeasureSpec)
        var heightMode = MeasureSpec.getMode(heightMeasureSpec)

        // 确定的值 不需要计算
        var width = MeasureSpec.getSize(widthMeasureSpec)
        var height = MeasureSpec.getSize(heightMeasureSpec)
        if (widthMode == MeasureSpec.AT_MOST) {
            width = defalutWidthAndHeight.sp2px(context)
        }

        if (heightMode == MeasureSpec.AT_MOST) {
            height = defalutWidthAndHeight.sp2px(context)
        }

        setMeasuredDimension(if (width > height) height else width, if (width > height) height else width)


    }


    override fun onDraw(canvas: Canvas) {

        // Step 6.1 画外圆
        var rectF = RectF(
            mBorderWidth.toFloat(), mBorderWidth.toFloat(), width.toFloat() - mBorderWidth,
            height.toFloat() - mBorderWidth
        )


        val center = width / 2
        // userCenter 设置为ture 是个闭合的圆
        canvas.drawArc(rectF, 135f, 270f, false, mOuterPaint)

        // Step 6.2 画内圆

        if (maxValue == 0) return
        val progress = mCurrentValue / maxValue.toFloat()
        canvas.drawArc(rectF, 135f, (270f * progress), false, mInnerPaint)

        // Step 6.3 画文字
        val bounds = Rect()
        mTextPaint.getTextBounds("$mCurrentValue", 0, "$mCurrentValue".length, bounds)
        var dx = center - bounds.width() / 2

        var fontMetrics = mTextPaint.fontMetrics
        var dy = (abs(fontMetrics.ascent) + fontMetrics.descent) / 2 - fontMetrics.descent
        val baseLine = dy + center

        canvas.drawText("$mCurrentValue", dx.toFloat(), baseLine, mTextPaint)


    }

    @Synchronized
    fun setMaxValue(value: Int) {
        this.maxValue = value
    }

    @Synchronized
    fun  setCurrentValue(value: Int) {
        this.mCurrentValue = value
        invalidate()
    }


}